package com.k1998.mybatis.service.impl;

import com.alibaba.fastjson.JSON;
import com.k1998.mybatis.bean.Journalism;
import com.k1998.mybatis.bean.Message;
import com.k1998.mybatis.dao.IJournalismDao;
import com.k1998.mybatis.service.IJournalismService;
import com.k1998.mybatis.utils.BatchInfo;
import com.k1998.mybatis.utils.JxUtils;
import com.k1998.mybatis.utils.QueryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
@Service
public class JournalismImpl implements IJournalismService {
    @Autowired
    private IJournalismDao iJournalismDao;
    @Override
    public String findJournalism(QueryInfo queryInfo) {
        // 查询到的结果数量
        int numbers = iJournalismDao.getJournalismCounts("%"+queryInfo.getQuery()+"%");
        // 第几条数据开始
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Journalism> journalism = iJournalismDao.findJournalism("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        System.out.println(journalism);
        res.put("numbers",numbers);
        res.put("data",journalism);
        res.put("code","success");
        System.out.println(res);
        String messages_json = JSON.toJSONString(res);
        return messages_json;
    }
    @Override
    public int insertJournalism(Journalism journalism) {
        int i = iJournalismDao.insertJournalism(journalism);
        String str = i >0?"success":"error";
        return i;
    }

    @Override
    public int batchDeleteJournalism(BatchInfo info) {
        List<String> list= JxUtils.stringToList(info.getIds());
        if(null==list){return 0;}
        int i = iJournalismDao.batchDeleteJournalism(list);
        return i;
    }
    @Override
    public int updateJournalismById(Journalism journalism) {
        int i=iJournalismDao.updateJournalismById(journalism);
        return i;
    }

    @Override
    public String getJournalismById(Journalism journalism) {
        Journalism journalismById = iJournalismDao.getJournalismById(journalism);
        String messages_json = JSON.toJSONString(journalismById);
        return messages_json;
    }
}
